
cmake_minimum_required(VERSION 3.12)
set(RB-COM_TEST_VERSION 0.1.0)
project(com-sample VERSION ${RB-COM_TEST_VERSION})
list(APPEND CMAKE_MODULE_PATH "$ENV{SDK_PATH}/opt/vrte/lib/cmake/com-communication-manager")

set(CMAKE_MODULE_PATH $ENV{RB_VRTE_dependencies}/lib/cmake/vrteb_fs/modules FORCE)

include_directories(
    $ENV{RB_VRTE_dependencies}/include
    $ENV{RB_VRTE_dependencies}/include/com
    )

find_package(core-types REQUIRED)
find_package(ecucfg-lib)
if(ecucfg-lib_FOUND)
    include_directories(${ECUCFG-LIB_INCLUDE_DIRS})
else()
    message(FATAL_ERROR "ecucfg-lib not found!")
endif()

find_package(log-framework-if REQUIRED)
if(LOG_FRAMEWORK_IF_FOUND)
    include_directories(${LOG_FRAMEWORK_IF_INCLUDE_DIRS})
    #Link with ${LOG_FRAMEWORK_IF_LIBRARIES}
else()
    message(FATAL_ERROR "log-framework-if not found!")
endif()

find_package(com-communication-manager CONFIG REQUIRED)
if(com-communication-manager_FOUND)
    include_directories(${COM_COMMUNICATION_MANAGER_INCLUDE_DIRS})
else()
    message(FATAL_ERROR "com-communication-manager not found!")
endif()

find_package(exm-executionclient-lib CONFIG REQUIRED)
if(exm-executionclient-lib_FOUND)
    message(STATUS "exm-executionclient-lib found")
    include_directories(${EXM_EXECUTIONCLIENT_LIB_INCLUDE_DIRS})
    #Link with ${EXM_EXECUTIONCLIENT_LIB_LIBRARIES}
else()
    message(FATAL_ERROR "exm-executionclient-lib not found!")
endif()

find_package(dia-diagnostic-manager CONFIG REQUIRED)
if(NOT dia-diagnostic-manager_FOUND)
    message(FATAL_ERROR "dia-diagnostic-manager not found!")
endif()

#Generate comapi,ecucfg and dsomeip.
set(SAMPLE_MANIFESTS ${CMAKE_SOURCE_DIR})
set(SAMPLE_COMMON_MANIFESTS ${CMAKE_SOURCE_DIR})


set(CPP_ONLY 0)

if(${CPP_ONLY} EQUAL 0)

    message(NOTICE "Generate comapi")
    execute_process (COMMAND bash -c
        "aubist-configurator-ap-cli -b ${CMAKE_SOURCE_DIR} -fp **.arxml \
        $(find ${SAMPLE_COMMON_MANIFESTS} -follow -name *.arxml | sed -e 's@^@\ -i\ @g') \
        comapi -a -o ${CMAKE_SOURCE_DIR}/gen/code"
        RESULT_VARIABLE ret
    )
    if(ret GREATER 0)
        message(FATAL_ERROR "Bad exit status (comapi)")
    endif()

    message(NOTICE "Generate ecucfg")
    execute_process (COMMAND bash -c
        "aubist-configurator-ap-cli -b ${CMAKE_SOURCE_DIR} -fp **.arxml \
        $(find ${SAMPLE_MANIFESTS} ${SAMPLE_COMMON_MANIFESTS} -follow -name *.arxml | sed -e 's@^@\ -i\ @g') \
        ecucfg -o ${CMAKE_SOURCE_DIR}/gen/ecucfg"
        RESULT_VARIABLE ret
    )
    if(ret GREATER 0)
        message(FATAL_ERROR "Bad exit status (ecucfg)")
    endif()

    message(NOTICE "Generate flatcfg")
    execute_process (COMMAND bash -c
        "aubist-configurator-ap-cli -b ${CMAKE_SOURCE_DIR} -fp **.arxml \
        $(find ${SAMPLE_MANIFESTS} ${SAMPLE_COMMON_MANIFESTS} -follow -name *.arxml | sed -e 's@^@\ -i\ @g') \
        flatcfg -o ${CMAKE_SOURCE_DIR}/gen/flatcfg"
        RESULT_VARIABLE ret
    )
    if(ret GREATER 0)
        message(FATAL_ERROR "Bad exit status (flatcfg)")
    endif()

    message(NOTICE "Generate dsomeip")
    execute_process (COMMAND bash -c
        "aubist-configurator-ap-cli -b ${CMAKE_SOURCE_DIR} -fp **.arxml \
        $(find ${SAMPLE_COMMON_MANIFESTS} -follow -name *.arxml | sed -e 's@^@\ -i\ @g') \
        dsomeip -o ${CMAKE_SOURCE_DIR}/gen/someipcfg"
        RESULT_VARIABLE ret
    )
    if(ret GREATER 0)
        message(FATAL_ERROR "Bad exit status (dsomeip)")
    endif()

    message(NOTICE "Generate dosoad")
    execute_process (COMMAND bash -c
        "aubist-configurator-ap-cli -b ${CMAKE_SOURCE_DIR} -fp **.arxml \
        $(find ${SAMPLE_COMMON_MANIFESTS} -follow -name *.arxml | sed -e 's@^@\ -i\ @g') \
        dosoad -o ${CMAKE_SOURCE_DIR}/gen/dosoad"
        RESULT_VARIABLE ret
    )
    if(ret GREATER 0)
        message(FATAL_ERROR "Bad exit status (dosoad)")
    endif()

    # generate flatbuffers configurations(.json, .fbs) for diag_demo_skeleton
    message(NOTICE "Generate flatbuffers configurations(.json, .fbs) for diag_demo_skeleton")
    execute_process (COMMAND bash -c
        "aubist-configurator-ap-cli -b ${CMAKE_SOURCE_DIR} -fp **.arxml \
        $(find ${SAMPLE_MANIFESTS} ${SAMPLE_COMMON_MANIFESTS} -follow -name *.arxml | sed -e 's@^@\ -i\ @g') \
        flatbuffers -dsl ${CMAKE_SOURCE_DIR}/AdaptiveApplication/diag_demo/etc/DIA_RBDIAG.ecucfgdsl -o ${CMAKE_SOURCE_DIR}/gen/flatbuffers"
        RESULT_VARIABLE ret
    )
    if(ret GREATER 0)
        message(FATAL_ERROR "Bad exit status (flatbuffers configurations)")
    endif()

    # generate flatbuffer binary from flatbuffers configuration
    message(NOTICE "Generate flatbuffer binary from flatbuffers configuration for diag_demo_skeleton")
    execute_process (COMMAND bash -c
        "flatc -o ${CMAKE_SOURCE_DIR}/gen/flatcfg/aapf_skeleton_swcl --binary \
        ${CMAKE_SOURCE_DIR}/gen/flatbuffers/dia_rbdiag_flatcfg.fbs \
        ${CMAKE_SOURCE_DIR}/gen/flatbuffers/xyl_skeleton_swcl/dia_rbdiag_diag_demo_skeleton__xyl_skeleton_swcl_flatcfg.json"
        RESULT_VARIABLE ret
    )
    if(ret GREATER 0)
        message(FATAL_ERROR "Bad exit status (flatbuffer binary)")
    endif()

    message(NOTICE "Generate phm")
    execute_process (COMMAND bash -c
        "aubist-configurator-ap-cli -b ${CMAKE_SOURCE_DIR} -fp **.arxml \
        $(find ${SAMPLE_COMMON_MANIFESTS} -follow -name *.arxml | sed -e 's@^@\ -i\ @g') \
        phm -o ${CMAKE_SOURCE_DIR}/gen/code"
        RESULT_VARIABLE ret
    )

    if(ret GREATER 0)
        message(FATAL_ERROR "Bad exit status (phm)")
    endif()

    message(NOTICE "Generate nm")
    execute_process (COMMAND bash -c
        "aubist-configurator-ap-cli -b ${CMAKE_SOURCE_DIR} -fp **.arxml \
        $(find ${SAMPLE_COMMON_MANIFESTS} -follow -name *.arxml | sed -e 's@^@\ -i\ @g') \
        nm -o ${CMAKE_SOURCE_DIR}/gen/someipcfg"
        RESULT_VARIABLE ret
    )
    if(ret GREATER 0)
        message(FATAL_ERROR "Bad exit status (nm)")
    endif()

    #Set the QNX flags at this level so that both Carma and tests are compiled for the same target.
    if(CMAKE_SYSTEM_NAME MATCHES QNX)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_QNX_SOURCE")
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_QNX_SOURCE")
    else(CMAKE_SYSTEM_NAME MATCHES QNX)
        add_compile_definitions(VFC_ENABLE_EXPERIMENTAL)
    endif(CMAKE_SYSTEM_NAME MATCHES QNX)

endif()

include_directories(${CMAKE_SOURCE_DIR}/ara/include)

##################################
#vrteb not working here with 20.02
##################################

add_subdirectory(AdaptiveApplication/receiver)
add_subdirectory(AdaptiveApplication/sender)
add_subdirectory(AdaptiveApplication/demo)
add_subdirectory(AdaptiveApplication/stm_ctl)
add_subdirectory(AdaptiveApplication/diag_demo)
